#include<stdio.h>
struct Tree
{
    int left;
    int right;
} T[200000];
int ans = 0;

void dfs(int id, int deep)
{
    if(id != 0)
    {
        if (ans < deep) ans = deep;
        dfs(T[id].left,deep+1);
        dfs(T[id].right,deep+1);
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n;i++)
    {
        scanf("%d %d",&T[i].left,&T[i].right);
    }
    dfs(1,1);
    printf("%d",ans);
}
